(function ($app) {
    $app.module('gtPartyApp').controller('pf_meetingTxthListCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $bsRouterState, AuthUser, $timeout, $excel, $partySelect, $filter) {
        $state.params = $bsRouterState.$params($scope);
        $scope.loginUserId = AuthUser.getUser().Id;
        $scope.isShow = false;
        $scope.moreTxt = "更多";
        $scope.title = "";
        $scope.partyLifeMeetingType = $state.params.partyLifeMeetingType;
        $scope.isAll = false;
        $scope.pageId = "pf_meetingTxthList";

        $scope.dataList = []; //列表数据
        $scope.treeparams = {
            isunfold: false,//组织结构树展开收缩
            selectdzzdm: "",
            selectdzzmc: "",
            defaultselected: $state.params.dzzdm
        };
        $scope.selectparams = {
            pageindex: 1,
            pagesize: 10,
            partyLifeMeetingType: $scope.partyLifeMeetingType,
            dzzdm: '',
            title: '',
            startDate: null,
            endDate: null
        };
        $scope.pageInfo = {ptotal: 0};

        $scope.getPartyLifeMeetingTypeList = function () {
            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {
                    dicTypeKey: 'PartyLifeMeetingType'
                }
            }).then(function (result) {
                $scope.title = result.data.filter(it => it.dickey == $scope.partyLifeMeetingType)[0].dicvalue;
            });
        };

        //加载列表数据
        $scope.loadData = function () {
            $http
            ({
                method: 'get', url: '../../api/partylife/meeting/getList', params: $scope.selectparams
            }).then(function (result) {
                $scope.dataList = result.data.item.list;
                $scope.pageInfo.ptotal = result.data.item.total;
            }, function (res) {
                $scope.showMsg('错误', '服务器错误');
            });
        };

        $scope.search = function () {
            $scope.loadData();
        };

        $scope.selectAll = function () {
            $scope.isAll = !$scope.isAll;
            angular.forEach($scope.dataList, function (item) {
                item.checked = $scope.isAll ? 1 : 0;
            });
        };

        //定义模态框
        pfMeetingTxth_editCtrl.$inject = ['$scope', '$http', '$alert', '$loading','$partySelect','AuthUser'];
        pfMeetingTxth_editModal = $modal({
            resolve: {
                load: ['$ocLazyLoad', function ($ocLazyLoad) {
                }]
            },
            scope: $scope,
            controller: pfMeetingTxth_editCtrl,
            templateUrl: '../main/partylife/txth/edit.html',
            show: false,
            animation: 'am-fade-and-slide-top'
        });
        $scope.edit = function (id) {
            pfMeetingTxth_editModal.params = {
                id: id,
                partyLifeMeetingType: $scope.partyLifeMeetingType,
                title: (id ? "修改" : "新增") + $scope.title
            };
            pfMeetingTxth_editModal.parentLoad = $scope.loadData;
            pfMeetingTxth_editModal.$promise.then(pfMeetingTxth_editModal.show);
        };

        $scope.export = function () {
            var params = "?1=1";
            for (var p in $scope.selectparams) {
                if ($scope.selectparams[p] !== '' && $scope.selectparams[p] !== null)
                    params += ("&" + p + "=" + encodeURI($scope.selectparams[p]));
            }
            window.open('../../api/partylife/meeting/exportTxth' + params);
        };

        $scope.delete = function (id) {
            var ids = [];
            if (id)
                ids.push(id);
            else
                ids = $scope.dataList.filter(it => it.checked == true).map(it => it.partylifemeetingid);

            if (ids.length == 0) {
                $scope.showMsg('消息', "请选择需要删除的数据");
                return false;
            }

            if (confirm("确定删除？")) {
                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/partylife/meeting/delete",
                    data: {
                        ids: JSON.stringify(ids)
                    }
                }).then(function (result) {
                    $loading.hide();
                    if (result.data.success) {
                        $scope.showMsg('成功', result.data.msg);
                        //重新刷新列表
                        $scope.loadData();
                    } else {
                        $scope.showMsg('失败', result.data.msg);
                    }
                }, function (resp) {
                    $scope.showMsg('错误', '服务器错误');
                });
            }
        };

        $scope.$watch("selectparams.pageindex", function (newVal, oldVal) {
            if ($scope.pageInfo.ptotal > 0) {
                $scope.loadData();
            }
        });
        $scope.$watch("treeparams.selectdzzdm", function (newVal, oldVal) {
            if (newVal != "") {
                if ($scope.treeparams.defaultselected != "" && $scope.treeparams.defaultselected != null &&
                    $scope.treeparams.defaultselected != $scope.treeparams.selectdzzdm)
                    return;

                $scope.selectparams.dzzdm = $scope.treeparams.selectdzzdm;

                $scope.loadData();
            }
        });
        $scope.setisunfold = function () {
            $scope.treeparams.isunfold = !$scope.treeparams.isunfold;
            $scope.$emit('menudatas.toggle', !$scope.treeparams.isunfold);
        };
        $scope.isShowClick = function () {
            $scope.isShow = !$scope.isShow;
            if ($scope.moreTxt == "更多") {
                $scope.moreTxt = "收起";
            } else if ($scope.moreTxt == "收起") {
                $scope.moreTxt = "更多";
            }
        };

        $scope.loadData();
        $scope.getPartyLifeMeetingTypeList();
    });
})(angular);